/**
 * 代码生成API服务
 */
import { request } from "@/http/axios"

export interface CodeGenerationRequest {
  templateId: number
  projectName: string
  packageName: string
  author: string
  description?: string
  tableIds: number[]
  features: {
    enableSwagger: boolean
    enableSecurity: boolean
    enableRedis: boolean
    enableValidation: boolean
    enablePageHelper: boolean
  }
  databaseConfig?: {
    host: string
    port: number
    database: string
    username: string
    password: string
  }
}

export interface GenerationProgress {
  taskId: string
  status: "PENDING" | "RUNNING" | "COMPLETED" | "FAILED"
  progress: number
  message: string
  startTime: string
  endTime?: string
  downloadUrl?: string
}

export interface GeneratedProject {
  id: number
  taskId: string
  projectName: string
  templateName: string
  status: "PENDING" | "RUNNING" | "COMPLETED" | "FAILED"
  progress: number
  filePath?: string
  downloadUrl?: string
  createdAt: string
  updatedAt: string
}

export type CodeGenerationResponse = ApiResponseData<{ taskId: string }>
export type GenerationProgressResponse = ApiResponseData<GenerationProgress>
export type GeneratedProjectListResponse = ApiResponseData<{
  records: GeneratedProject[]
  total: number
  current: number
  size: number
}>

export class CodeGenerationApiService {
  /**
   * 启动代码生成任务
   */
  static async generateCode(request: CodeGenerationRequest): Promise<CodeGenerationResponse> {
    return request<CodeGenerationResponse>({
      url: "codeGeneration/generate",
      method: "post",
      data: request
    })
  }

  /**
   * 获取生成进度
   */
  static async getProgress(taskId: string): Promise<GenerationProgressResponse> {
    return request<GenerationProgressResponse>({
      url: `codeGeneration/progress/${taskId}`,
      method: "get"
    })
  }

  /**
   * 获取生成历史
   */
  static async getGenerationHistory(params?: {
    current?: number
    size?: number
    status?: string
    projectName?: string
  }): Promise<GeneratedProjectListResponse> {
    return request<GeneratedProjectListResponse>({
      url: "codeGeneration/history",
      method: "get",
      params
    })
  }

  /**
   * 下载生成的项目
   */
  static async downloadProject(taskId: string): Promise<Blob> {
    return request<Blob>({
      url: `codeGeneration/download/${taskId}`,
      method: "get",
      responseType: "blob"
    })
  }

  /**
   * 删除生成的项目
   */
  static async deleteProject(id: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `codeGeneration/project/${id}`,
      method: "delete"
    })
  }

  /**
   * 重新生成项目
   */
  static async regenerateProject(id: number): Promise<CodeGenerationResponse> {
    return request<CodeGenerationResponse>({
      url: `codeGeneration/regenerate/${id}`,
      method: "post"
    })
  }

  /**
   * 预览生成的代码文件
   */
  static async previewCode(taskId: string, filePath: string): Promise<ApiResponseData<{ content: string }>> {
    return request<ApiResponseData<{ content: string }>>({
      url: `codeGeneration/preview/${taskId}`,
      method: "get",
      params: { filePath }
    })
  }

  /**
   * 获取项目文件结构
   */
  static async getProjectStructure(taskId: string): Promise<ApiResponseData<any>> {
    return request<ApiResponseData<any>>({
      url: `codeGeneration/structure/${taskId}`,
      method: "get"
    })
  }

  /**
   * 取消生成任务
   */
  static async cancelGeneration(taskId: string): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `codeGeneration/cancel/${taskId}`,
      method: "post"
    })
  }
}
